package com.example.oasystem.controller.file;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.oasystem.dao.file.AoaFileListDao;
import com.example.oasystem.dao.file.AoaFilePathDao;
import com.example.oasystem.entity.file.AoaFileList;
import com.example.oasystem.entity.file.AoaFilePath;
import com.example.oasystem.entity.user.AoaUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FileAjaxController {
    @Autowired
    private AoaFileListDao aoaFileListDao;
    @Autowired
    private AoaFilePathDao aoaFilePathDao;
    @RequestMapping("filetypeload")
    public String filetypeload(HttpSession session,
                               @RequestParam("type") String type,
                               Model model){
        AoaUser user = (AoaUser)session.getAttribute("user");
        List<AoaFileList> fileLists = null;
        List<AoaFilePath> filePaths = null;
        switch (type) {
            case "share":
                fileLists = aoaFileListDao.selectList(new QueryWrapper<AoaFileList>().eq("file_isshare",1L));
                model.addAttribute("files", fileLists);
                model.addAttribute("isshare", 1);
                model.addAttribute("isload",1);
                break;
            case "trash":
                filePaths = aoaFilePathDao.selectList(new QueryWrapper<AoaFilePath>().eq("path_user_id",user.getUserId())
                        .eq("path_istrash",1L));
                AoaFilePath userrootpath = aoaFilePathDao.selectOne(new QueryWrapper<AoaFilePath>().eq("path_user_id", user.getUserId())
                        .eq("parent_id",1L));
                fileLists = aoaFileListDao.selectList(new QueryWrapper<AoaFileList>().eq("file_user_id",user.getUserId())
                        .eq("file_istrash",1L).eq("path_id",userrootpath.getPathId()));

                model.addAttribute("paths", filePaths);
                model.addAttribute("files", fileLists);
                model.addAttribute("istrash", 1);
                model.addAttribute("isload",1);
                break;
            case "video":
                fileLists = aoaFileListDao.selectList(new QueryWrapper<AoaFileList>().like("content_type","video"));
                model.addAttribute("files",fileLists);
                model.addAttribute("isload",1);
                break;
            case "yasuo":
                fileLists = aoaFileListDao.selectList(new QueryWrapper<AoaFileList>().like("content_type","application"));
                model.addAttribute("files",fileLists);
                model.addAttribute("isload",1);
                break;
            default:
                break;
        }
        model.addAttribute("type", type);
        return "file/filetypeload";
    }

    @RequestMapping("/filereturnback")
    public String filereturnback(HttpSession session,
                                 @RequestParam("type") String type,
                                 @RequestParam(value="checkpathids[]",required=false) List<Long> checkpathids,
                                 @RequestParam(value="checkfileids[]",required=false) List<Long> checkfileids,
                                 Model model){
        System.out.println("checkpathids = " + checkpathids);
        System.out.println("checkfileids = " + checkfileids);
        if (checkfileids!=null &&!checkfileids.isEmpty()) {
            // 文件还原
            for (Long checkfileid : checkfileids) {
                AoaFileList returnFile = new AoaFileList();
                returnFile.setFileId(checkfileid);
                returnFile.setFileIstrash(0L);
                aoaFileListDao.updateById(returnFile);
            }
        }
        if (checkpathids!=null && !checkpathids.isEmpty()) {
            for (Long checkpathid : checkpathids) {
                List<AoaFilePath> allChildPaths = aoaFilePathDao.selectAllChildPaths(checkpathid);
                // 把文件夹中 所有子文件夹 还原
                for (AoaFilePath childPath : allChildPaths) {
                    AoaFilePath returnPath = new AoaFilePath();
                    returnPath.setPathId(childPath.getPathId());
                    returnPath.setPathIstrash(0L);
                    aoaFilePathDao.updateById(returnPath);
                    // 把文件夹中 所有子文件夹中 所有文件 还原
                    List<AoaFileList> returnPathFiles = aoaFileListDao.selectList(new QueryWrapper<AoaFileList>().eq("path_id", childPath.getPathId()));
                    for (AoaFileList returnPathFile : returnPathFiles) {
                        AoaFileList returnFile = new AoaFileList();
                        returnFile.setFileId(returnPathFile.getFileId());
                        returnFile.setFileIstrash(0L);
                        aoaFileListDao.updateById(returnFile);
                    }
                }
            }
        }
        model.addAttribute("type", type);
        model.addAttribute("isload",1);
        return "forward:/filetypeload";
    }
    @RequestMapping("findfileandpath")
    public String findfileandpath(HttpSession session,
                                  @RequestParam(value = "findfileandpath",required=false) String findfileandpath,
                                  @RequestParam(value = "type",defaultValue="all") String type,
                                  Model model){
        System.out.println("进入查找");
        System.out.println("查找！~~~~~~");
        AoaUser user = (AoaUser) session.getAttribute("user");
        String findlike = "%" +findfileandpath+ "%";
        AoaFilePath fpath = aoaFilePathDao.findFileByUserName(user.getUserName());
        String contenttype;
        List<AoaFileList> fileLists = null;
        List<AoaFilePath> filePaths = null;
        System.out.println(type);
        if (findfileandpath==null||findfileandpath=="")
        {
            return "forward:filemanage";
        }
        AoaFilePath userrootpath = aoaFilePathDao.selectOne(new QueryWrapper<AoaFilePath>().eq("path_user_id", user.getUserId()).eq("parent_id",1L));
        List<AoaFilePath> mcpaths = aoaFilePathDao.selectmcpaths(userrootpath.getPathId());
        filePaths = aoaFilePathDao.findByPathUserIdAndPathIstrashAndPathNameLikeAndParentIdNota(user.getUserId(),  findlike);
        fileLists = aoaFilePathDao.findByUserAndFileIstrashAndFileNameLike(user.getUserId(),findlike);

        model.addAttribute("userrootpath",userrootpath);
        model.addAttribute("nowpath",userrootpath);
        model.addAttribute("files", fileLists);
        model.addAttribute("userid",user.getUserId());
        model.addAttribute("paths", filePaths);
        model.addAttribute("mcpaths",mcpaths);
        model.addAttribute("isload",1);
        model.addAttribute("type", type);
        return "file/filemanage";

    }

}
